<?php
class userController extends AController{	
	private $tableName='SYSUSER';

	public function indexAction() {
		$page = $this->request->getGetParameter('p',1);
		$pagesize = $this->request->getGetParameter('ps',9);
		$select = array();
		$select['ROLE_ID'] = JYFilterUtils::unfilterText($this->request->getGetParameter('grade'));
		$select['title'] = JYFilterUtils::unfilterText($this->request->getGetParameter('key'));
		if(isset($_POST['sel_user'])){
			$select['ROLE_ID'] = JYFilterUtils::unfilterText($this->request->getPostParameter('grade'));
			$select['title'] = JYFilterUtils::unfilterText($this->request->getPostParameter('key'));
		}
		$allgroup = $this->getAllGroupName();
		foreach ($allgroup as $v){
			$groups[$v['RID']] = $v['RN'];
		}
		unset($allgroup);
		$alluser = $this->getAllUserInfo($page,$pagesize,$select);
		$tolNum = $this->getTolUser($select);
		$state = ATools::getConfig('app.user.state');
		$this->groups = $groups;
		$this->alluser = $alluser;
		$this->vkey = $select['title'];
		$this->grade = $select['ROLE_ID'];
		$this->state = $state;
		$this->page = $page;
		$this->pagesize = $pagesize;
		$this->tolNum = $tolNum['NUM'];
		return $this->render();
	}
	
	public function addAction() {
		$id = $this->request->getGetParameter('id');
		if(isset($_POST['add_user'])){
//			var_dump($_POST);exit;
			if(empty($id)){
				$upd['USER_NAME'] = $username = $this->request->getPostParameter('name');
				$password = $this->request->getPostParameter('pwd');
				if(empty($username) || empty($password)){
					$this->alert('请正确输入信息');exit;
				}
				if($this->checkUsername($username)){
					$this->alert('用户名已存在，请重新输入');exit;
				}
				$upd['PASSWORD'] = md5($password);
				$upd['TRUE_NAME'] = $this->request->getPostParameter('realname');
				$upd['UNIT_ID'] = $this->request->getPostParameter('unit');
				$upd['DOC_NO'] =  $this->request->getPostParameter('code');
				$upd['ROLE_ID'] =  $this->request->getPostParameter('group');
				$upd['DEP_NO'] =  $this->request->getPostParameter('dep');
				$upd['STATE'] = 1;
				$upd['UPD_TIME'] = date('Y-m-d H:i:s');
				$upd['UPD_USER'] = $this->username;
				if($this->insertUser($upd)){
					$this->alert('添加成功');exit;
				}
				$this->alert('系统繁忙，请稍后再试');exit;
			}else{
				$upd['USER_NAME'] = $username = $this->request->getPostParameter('name');
				if(empty($username)){
					$this->alert('请正确输入用户名');exit;
				}
				$upd['TRUE_NAME'] = $this->request->getPostParameter('realname');
				$upd['UNIT_ID'] = $this->request->getPostParameter('unit');
				$upd['DOC_NO'] =  $this->request->getPostParameter('code');
				$upd['ROLE_ID'] =  $this->request->getPostParameter('group');
				$upd['DEP_NO'] =  $this->request->getPostParameter('dep');
				$upd['STATE'] = 1;
				$upd['UPD_TIME'] = date('Y-m-d H:i:s');
				$upd['UPD_USER'] = $this->username;
				if($this->updateUser($id,$upd)){
					$this->alert('操作成功');exit;
				}
				$this->alert('系统繁忙，请稍后再试');exit;
			}
		}
		$userinfo = array();
		if(!empty($id)){
			$userinfo = $this->getUserInfoById($id);
		}
		$allunit = $this->getAllUnitName();
//		var_dump($allunit);
//		$allgroup = $this->getAllGroupName();
		if(!empty($id) && count($userinfo)>0){
			$one_id = $userinfo['UNIT_ID'];
			$allDep = $this->getAllDepById($one_id);
		}else{
			$one_id = $allunit[0]['UID'];
			$allDep = $this->getAllDepById($one_id);
		}
		$allgroup = $this->getAllGroupName($one_id);
		foreach ($allunit as $v){
			$allUnit[$v['UID']] = $v['UN'];
		}
		foreach ($allgroup as $v){
			$groups[$v['RID']] = $v['RN'];
		}
		foreach ($allDep as $v){
			$alldep[$v['DID']] = $v['DN'];
		}
		unset($allgroup);
		unset($allunit);
		unset($allDep);
		$this->alldep = $alldep;
		$this->allUnit = $allUnit;
		$this->groups = $groups;
		$this->id = $id;
		$this->unit_one = $one_id;
		$this->userinfo = $userinfo;
		return $this->render();
	}
	public function resetpwdAction(){
		$id = $this->request->getGetParameter('id');
		if(isset($_POST['edit_pwd'])){
				$password = $this->request->getPostParameter('newpwd');
				if(empty($password)){
					$this->alert('密码不能为空，请重新输入');exit;
				}
				$upd['PASSWORD'] = md5($password);
				if($this->updateUser($id,$upd)){
					$this->alert('操作成功');exit;
				}
				$this->alert('系统繁忙，请稍后再试');exit;
		}
		$userinfo = $this->getUserInfoById($id);
		
		$this->id = $id;
		$this->userinfo = $userinfo;
		return $this->render();
	}
	public function deluserAction(){
		$id = $this->request->getGetParameter('id');
		if($this->delUserById($id)){
			return json_encode(array('msg'=>'suc','code'=>1000));
		}
		return json_encode(array('msg'=>'fail','code'=>1001));
	}
	public function getdepAction(){
		$id = $this->request->getGetParameter('id');
		$alldep = $this->getAllDepById($id);
		$allGroup = $this->getAllGroupName($id);
		$ret['dep'] = $alldep;
		$ret['group'] = $allGroup;
		return json_encode($ret);
	}
	function checkUsername($username){
		$ret = JYDB::find($this->tableName,'USER_NAME=\''.$username.'\'','count(*) num');
		if($ret['NUM']>0){
			return true;
		}
		return false;
	}
	function getAllUnitName(){
		return JYDB::findAll('UNIT','unit_state>0','UNIT_ID "UID",UNIT_NAME UN','UNIT_SPELL');
	}
	function getAllGroupName($unid=''){
		$where = '';
		if(!empty($unid) && $unid>0){
			$where = ' and unit_id='.$unid;
		}
		return JYDB::findAll('ROLE','state>0'.$where,'ROLE_ID "RID",ROLE_NAME "RN"');
	}
	function getAllDepById($id){
		$where = '';
		if(!empty($id) && $id>0){
			$where = 'unit_id='.$id;
		}
		return JYDB::findAll('DEP',$where,'DEP_ID DID,DEP_NAME DN');
	}
	function getUserInfoById($id){
		return JYDB::find($this->tableName,'USER_ID='.$id);
	}
	function getAllUserInfo($page,$pagesize,$select){
		if($this->unid<0){
			$where = 'state>0 ';
		}else{
			$where = 'state>0 and unit_id='.$this->unid;
		}
		
		$fields = '*';
		if(count($select)>0){
			foreach ($select as $k=>$v){
				if(!empty($v)){
					if($k=='title'){
						$where .= ' and USER_NAME like \'%'.$v.'%\' or TRUE_NAME like \'%'.$v.'%\' or DOC_NO like \'%'.$v.'%\'';
						continue;
					}
					$where .= ' and '.$k.'='.$v;
				}
			}
		}
		return JYDB::findAll($this->tableName,$where,$fields,'ROLE_ID,upd_time desc',$page,$pagesize);
	}
	function getTolUser($select){
		if($this->unid<0){
			$where = 'state>0 ';
		}else{
			$where = 'state>0 and unit_id='.$this->unid;
		}
		if(count($select)>0){
			foreach ($select as $k=>$v){
				if(!empty($v)){
					if($k=='title'){
						$where .= ' and USER_NAME like \'%'.$v.'%\' or TRUE_NAME like \'%'.$v.'%\' or DOC_NO like \'%'.$v.'%\'';
						continue;
					}
					$where .= ' and '.$k.'='.$v;
				}
			}
		}
		return JYDB::find($this->tableName,$where,'count(*) num');
	}
	function insertUser($arr){
		return JYDB::create($this->tableName,$arr,'USER_ID');
	}
	function updateUser($id,$upd){
		return JYDB::update($this->tableName,'USER_ID='.$id,$upd);
	} 
	function delUserById($id){
		return JYDB::update($this->tableName,'doc_no>0 and user_id='.$id,array('state'=>0));
	}
}